var app = angular.module("app", ['ngCookies', 'ngTouch', 'angucomplete']);

app.config(function ($httpProvider) {
	
	$httpProvider.defaults.transformRequest = function(data, headersGetter) {
		return JSON.stringify(data);
	};
	
	$httpProvider.defaults.transformResponse = function(data, headersGetter) {
		var response = JSON.parse(data);
		
		if (response.result == 3) {
			Message.error("错误", response.message, function() {
				window.top.location.href = "/logout";
			});
			response = {};
		}
		
		return response;
	};
});


/**
 * Usage: <input datepicker
 * 	             type="text" 
 *               ng-model="expireTime" 
 *               date-format="yyyy-MM-dd" 
 * 			     name="expireTime" 
 *               id="expireTime" 
 *               min-date="#F{$dp.$D(\'sellTime\')}"
 *               max-date="#F{$dp.$D(\'expireTime\')}">
 */
app.directive('datepicker', function($parse) {
	return {
		restrict: 'A',
		link : function(scope, element, attrs) {
			var form = element[0].form.name;
			var field = element[0].name;
			
			element.addClass('date-picker');
			element.bind('click', function() {
				WdatePicker({
					readOnly : true,
					dateFmt : attrs.dateFormat || 'yyyy-MM-dd',
					onpicked : function() {
						/* 修改ngModel的方法1
						 * 注意：这种方法只修改ngModel值，不修改form字段的值及form状态，不触发form的校验
						 */
						// var model = $parse(attrs.ngModel);
						// model.assign(scope,this.value);
						// scope.$apply();

						/* 修改ngModel的方法2
						 * 注意：这种方法只修改ngModel值，不修改form字段的值及form状态，不触发form的校验
						 */
						// scope[attrs.ngModel] = this.value;
						// scope.$apply();
						
						/* 修改ngModel的方法3
						 * 修改form字段的值及form状态，触发form的校验，触发watch事件，继而修改ngModel
						 */
						scope[form][field].$setViewValue(this.value);
						scope.$apply();
					},
					oncleared : function() {
						scope[form][field].$setViewValue('');
						scope.$apply();
					},
					minDate: attrs.minDate,
					maxDate: attrs.maxDate
				});
			});
		}
	};
});

var Pagination = {
	initialize : function(findPageUrl, $scope, $http, pageSize, callback) {

		// 查询
		$scope.search = function() {
			$scope.condition = Object.merge({
				pageSize : $scope.condition.pageSize
			}, $scope.formCondition);

			$scope.findPage(1);
		};

		// 排序
		$scope.sortBy = function(sortProperty, gbkSortProperty) {
			var currentProperty = $scope.condition.sortProperty;
			var currentDirection = $scope.condition.sortDirection;
			$scope.condition.sortDirection = (currentProperty != sortProperty || currentDirection == "asc" ? "desc" : "asc");
			$scope.condition.sortProperty = sortProperty;
			$scope.condition.gbkSortProperty = gbkSortProperty!=false;
			$scope.findPage(1);
		};

		$scope.direction = function(property) {
			var cssClass = $scope.condition.sortProperty == property ? null : 'fa fa-none';
			cssClass = cssClass || ($scope.condition.sortDirection == 'desc' ? 'fa fa-sort-alpha-desc' : 'fa fa-sort-alpha-asc');
			return cssClass;
		};

		// 分页显示
		$scope.findPage = function(page, pageCount) {
			
			$scope.condition.page = page;
			
			if ($scope.condition.page < 1) {
				$scope.condition.page = 1;
			}
			if ($scope.condition.page > pageCount) {
				$scope.condition.page = pageCount;
			}
			
			$http.post(findPageUrl, $scope.condition).success(function (response) {
				$scope.page = response.data || {};
				$scope.condition.page = $scope.page.page;
				response.success || Message.error("错误", response.message || "查询失败！");
				callback && callback();
			});
		};
		
		$scope.condition = {
			pageSize : pageSize || 15,
			page : 1
		};

		$scope.formCondition = {};
	}
};